package com.meshnetics.gb.stack.mac.type;

import com.meshnetics.gb.type.impl.zcl.Bitmap;

/**
 * The specification of pending addresses used in MACBeaconNotifyIndication.
 *
 * @author AKovylin
 */
public class PendingAddressSpec extends Bitmap {
    public PendingAddressSpec(int shortAddressesPending, int extendedAddressesPending) {
        this(TypePendingAddressSpec.TYPE, new byte[]{0});
        if (shortAddressesPending < 0 || shortAddressesPending > 7) {
            throw new IllegalArgumentException("invalid shortAddressesPending");
        }
        if (extendedAddressesPending < 0 || extendedAddressesPending > 7) {
            throw new IllegalArgumentException("invalid extendedAddressesPending");
        }
        if (shortAddressesPending + extendedAddressesPending > 7) {
            throw new IllegalArgumentException("maximum 7 addresses allowed in total");
        }
        super.setBits(0, 3, shortAddressesPending);
        super.setBits(4, 7, extendedAddressesPending);
    }

    PendingAddressSpec(TypePendingAddressSpec type, byte[] value) {
        super(type, value);
        if (value == null) {
            throw new NullPointerException("value is null");
        }
        if (value.length != 1) {
            throw new IllegalArgumentException("invalid value");
        }
        if (getBit(3) || getBit(7)) {
            throw new IllegalArgumentException("reserved bit(s) is(are) set");
        }
    }

    public int getShortAddressesPending() {
        return getBits(0, 3);
    }

    public int getExtendedAddressesPending() {
        return getBits(4, 7);
    }
}
